package rollingCounter

//Avg Iterates through the iter, returning the average of all values
func Avg(iter Iterator) float64 {
	sum := 0.0
	count := 0

	for !iter.Ending() {
		b := iter.Get()
		for _, v := range b.Points {
			sum += v
		}
		count += b.Count
	}

	return sum / float64(count)
}

//Min Iterates through the iterator, returning the minimal value
func Min(iter Iterator) float64 {
	min := 0.0
	started := false

	for !iter.Ending() {
		b := iter.Get()
		for _, v := range b.Points {
			if !started {
				min = v
				started = true
				continue
			}
			if v < min {
				min = v
			}
		}
	}

	return min
}


//Max Iterates through the iterator, returning the maximum value
func Max(iter Iterator) float64 {
	max := 0.0
	started := false

	for !iter.Ending() {
		b := iter.Get()
		for _, v := range b.Points {
			if !started {
				max = v
				started = true
				continue
			}
			if v > max {
				max = v
			}
		}
	}

	return max
}

//Sum Iterates through the iterator, returning the sum of all values
func Sum(iter Iterator) float64 {
	sum := 0.0

	for !iter.Ending() {
		b := iter.Get()
		for _, v := range b.Points {
			sum += v
		}
	}

	return sum
}

//Count Iterates through the iterator, returning the count of items
func Count(iter Iterator) float64 {
	count := 0

	for !iter.Ending() {
		b := iter.Get()
		count += b.Count
	}

	return float64(count)
}